package cn.cjx.basic.listener;


import cn.cjx.system.service.IPermissionScanService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * @author: cjx
 * @Date: 2022/5/15
 */
@WebListener//申明这是自定义的WebListener,交给spring容器
@Slf4j
public class PermissionScanInitListener  implements ServletContextListener {

	@Autowired
	private IPermissionScanService permissionScanService;
	/**
	 * 该方法在spring容器初始化结束之后调用
	 * @author 陈吉祥
	 * @Date 2022/5/15
	 * 由于如果在该方法内写很多代码会降低项目启动效率，
	 * 故使用一个异步线程，在service中来实现  监听加权限
	 */
	@Override
	public void contextInitialized(ServletContextEvent sce) {

		new Thread(new Runnable() {
			@Override
			public void run() {
				log.info("权限开始初始化");
				permissionScanService.permissionScan();
				log.info("权限初始化完成");
			}
		}).start();
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {

	}
}
